<?php


namespace app\mobile\controller;


use think\Db;
use think\facade\Request;

class Client extends LoggingBase
{
    /**
     * 客戶列表
     */
    public function mb_List()
    {
        $Type = Request::has('Type')?Request::param('Type'):0;  //0：我的客戶，1：公海客戶，2：公司客戶
        $Page = Request::has('Page') ? Request::param('Page') : 1;
        $Limit = Request::has('Limit') ? Request::param('Limit') : 10;
        $Keyword = Request::has('Keyword') ? Request::param('Keyword') : '';
        $TimeCreated = Request::has('TimeCreated') ? Request::param('TimeCreated') : '';

        $Where = '';
        if ($Keyword != '') {
            $Where .= " AND (Name LIKE '%$Keyword%' OR Contacts LIKE '%$Keyword%' OR Phone LIKE '%$Keyword%') ";
        }
        if($TimeCreated!=''){
            $Where .= " AND DATE_FORMAT(TimeCreated,'%Y-%m-%d')='$TimeCreated' ";
        }

        $UserID = $this->m_User->ID;
        $CompanyID = $this->m_User->CompanyID;

        if($Type == 0){
            $Where .= " AND OwnerID='$UserID' AND OwnerType='1' ";
        } else if($Type == 1){
            $Where .= " AND OwnerID='$CompanyID' AND OwnerType='0' ";
        } else if($Type == 2){
            $Where .= " AND (OwnerID='$CompanyID' AND OwnerType='0' OR OwnerID='$UserID' AND OwnerType='1') ";
        }

        $Count = Db::table('client')->where("IsDeleted='0'  $Where ")->count();
        $Data = Db::table('client')->where("IsDeleted='0' $Where ")->page($Page, $Limit)->order('ID', 'DESC')->select();

        foreach ($Data as $Key => $Val) {
            $Data[$Key]['Source'] = $this->GetSourceName($Val['Source']);
            $Data[$Key]['IndustryName'] = $this->GetIndustryName($Val['Industry']);
            $Data[$Key]['LevelName'] = $this->GetLevelName($Val['Level']);
            $Data[$Key]['checked'] = false;
        }
        mb_OutMessage(ErrCode_Normal, '', $Data, $Count);
    }

    /**
     * 客戶詳情
     */
    public function mb_Detail()
    {
        $ClientID = Request::has('ClientID') ? Request::param('ClientID') : 0;
        if (!$ClientID) mb_OutMessage(ErrCode_Toast, '参数有误！');

        $Data = Db::table('client')->field('ID AS ClientID,Name,Level,Industry,Source,Contacts,Phone,Web,Next,Last,Remark,Email,Address,Deal,Follow')->where("IsDeleted='0' AND ID='$ClientID'")->find();
        if ($Data == null) mb_OutMessage(ErrCode_Toast, '该客户不存在');

        $Data['DealName'] = $this->GetDealName($Data['Deal']);
        $Data['LevelName'] = $this->GetLevelName($Data['Level']);
        $Data['IndustryName'] = $this->GetIndustryName($Data['Industry']);
        $Data['SourceName'] = $this->GetSourceName($Data['Source']);
        mb_OutMessage(ErrCode_Normal, '', $Data);
    }

    /**
     * 添加或編輯客戶
     */
    public function mb_Submit()
    {
        $Type = Request::has('Type') ? Request::param('Type') : 0;//0:我的客户，1：公海客户，2：公司客户
        $ID = Request::has('ID') ? Request::param('ID') : 0;//0：新增，Other：編輯
        $Name = Request::has('Name') ? Request::param('Name') : '';
        $Level = Request::has('Level') ? Request::param('Level') : '';
        $Industry = Request::has('Industry') ? Request::param('Industry') : '';
        $Source = Request::has('Source') ? Request::param('Source') : '';
        $Contacts = Request::has('Contacts') ? Request::param('Contacts') : '';
        $Phone = Request::has('Phone') ? Request::param('Phone') : '';
        $Web = Request::has('Web') ? Request::param('Web') : '';
        $Next = Request::has('Next') ? Request::param('Next') : '';
        $Last = Request::has('Last') ? Request::param('Last') : '';
        $Remark = Request::has('Remark') ? Request::param('Remark') : '';
        $Email = Request::has('Email') ? Request::param('Email') : '';
        $Address = Request::has('Address') ? Request::param('Address') : '';
        $Deal = Request::has('Deal') ? Request::param('Deal') : '';
        $Follow = Request::has('Follow') ? Request::param('Follow') : '';

        if(!$Name || !$Contacts || !$Phone)mb_OutMessage(ErrCode_Toast,'参数有误');

        Db::startTrans();
        try {
            $Data = array(
                'Name'=>$Name,
                'Level'=>$Level,
                'Industry'=>$Industry,
                'Source'=>$Source,
                'Contacts'=>$Contacts,
                'Phone'=>$Phone,
                'Web'=>$Web,
                'Next'=>$Next,
                'Last'=>$Last,
                'Remark'=>$Remark,
                'Email'=>$Email,
                'Address'=>$Address,
                'Deal'=>$Deal,
                'Follow'=>$Follow
            );
            if($ID == 0) {
                //新增客户
                if($Type == 0){
                    //新增到我的客户
                    $Data['OwnerID'] = $this->m_User->ID;
                    $Data['OwnerType'] = 1;
                } else {
                    //新增到公海客户
                    $Data['OwnerID'] = $this->m_User->CompanyID;
                    $Data['OwnerType'] = 0;
                }
                Db::table('client')->insert($Data);
            } else {
                //编辑客户
                $Data['ID'] = $ID;
                Db::table('client')->update($Data);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal,'保存成功');
    }

    /**
     * 转移客户
     */
    public function mb_Transfer()
    {
        $ClientIDs = Request::has('ClientIDs') ? Request::param('ClientIDs') : '';  //客户IDs
        $Type = Request::has('Type') ? Request::param('Type') : 0;  //0:我的客户，1：公海客户
        $ToID = Request::has('ToID') ? Request::param('ToID') : '';
        if ($ClientIDs == '') mb_OutMessage(ErrCode_Toast, '参数有误');
        Db::startTrans();
        try {
            if($Type == 0){
                //轉移到個人客戶
                Db::table('client')->where("ID IN ( $ClientIDs )")->update(array(
                    'OwnerID'=>$ToID,
                    'OwnerType'=>1
                ));
            } else {
                //轉移到公海客戶
                Db::table('client')->where("ID IN ( $ClientIDs )")->update(array(
                    'OwnerID'=>$this->m_User->CompanyID,
                    'OwnerType'=>0
                ));
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal,'转移成功');
    }

    /**
     * 删除客户
     */
    public function mb_Delete()
    {
        $ClientIDs = Request::has('ClientIDs') ? Request::param('ClientIDs') : '';  //客户IDs
        Db::startTrans();
        try {
            Db::table('client')->where("ID IN ( $ClientIDs )")->update(array(
                'IsDeleted'=>1
            ));
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal,'删除成功');
    }

    private function GetDealName($Deal){
        $Name = '未成交';
        if($Deal == 1)$Name = '已成交';
        return $Name;
    }

    private function GetSourceName($Source)
    {
        $Name = '';
        switch ($Source) {
            case 1:
                $Name = '促销活动';
                break;
            case 2:
                $Name = '搜索引擎';
                break;
            case 3:
                $Name = '广告';
                break;
            case 4:
                $Name = '转介绍';
                break;
            case 5:
                $Name = '线上注册';
                break;
            case 6:
                $Name = '线上询价';
                break;
            case 7:
                $Name = '预约上门';
                break;
            case 8:
                $Name = '陌拜';
                break;
            case 9:
                $Name = '招商资源';
                break;
            case 10:
                $Name = '公司资源';
                break;
            case 11:
                $Name = '展会资源';
                break;
            case 12:
                $Name = '个人资源';
                break;
            case 13:
                $Name = '电话咨询';
                break;
            case 14:
                $Name = '邮件咨询';
                break;
            default:
                break;
        }
        return $Name;
    }

    private function GetIndustryName($Industry)
    {
        $Name = '';
        switch ($Industry) {
            case 1:
                $Name = 'IT/通信/电子/互联网';
                break;
            case 2:
                $Name = '金融业';
                break;
            case 3:
                $Name = '房产业';
                break;
            case 4:
                $Name = '商业服务';
                break;
            case 5:
                $Name = '贸易';
                break;
            case 6:
                $Name = '生产';
                break;
            case 7:
                $Name = '运输物品';
                break;
            case 8:
                $Name = '服务业';
                break;
            case 9:
                $Name = '文化传媒';
                break;
            case 10:
                $Name = '政府';
                break;
            case 11:
                $Name = '其他';
                break;
        }
        return $Name;
    }

    private function GetLevelName($Level)
    {
        $Name = 'A类（重点客户）';
        switch ($Level) {
            case 'B':
                $Name = 'B类（普遍客户）';
                break;
            case 'C':
                $Name = 'C类（非优先客户）';
                break;
        }
        return $Name;
    }

    private function GetSourceNum($Source)
    {
        $Name = 0;
        switch ($Source) {
            case '促销活动':
                $Name = 1;
                break;
            case '搜索引擎':
                $Name = 2;
                break;
            case '广告':
                $Name = 3;
                break;
            case '转介绍':
                $Name = 4;
                break;
            case '线上注册':
                $Name = 5;
                break;
            case '线上询价':
                $Name = 6;
                break;
            case '预约上门':
                $Name = 7;
                break;
            case '陌拜':
                $Name = 8;
                break;
            case '招商资源':
                $Name = 9;
                break;
            case '公司资源':
                $Name = 10;
                break;
            case '展会资源':
                $Name = 11;
                break;
            case '个人资源':
                $Name = 12;
                break;
            case '电话咨询':
                $Name = 13;
                break;
            case '邮件咨询':
                $Name = 14;
                break;
            default:
                break;
        }
        return $Name;
    }

    private function GetIndustryNum($Industry)
    {
        $Name = 0;
        switch ($Industry) {
            case 'IT/通信/电子/互联网':
                $Name = 1;
                break;
            case '金融业':
                $Name = 2;
                break;
            case '房产业':
                $Name = 3;
                break;
            case '商业服务':
                $Name = 4;
                break;
            case '贸易':
                $Name = 5;
                break;
            case '生产':
                $Name = 6;
                break;
            case '运输物品':
                $Name = 7;
                break;
            case '服务业':
                $Name = 8;
                break;
            case '文化传媒':
                $Name = 9;
                break;
            case '政府':
                $Name = 10;
                break;
            case '其他':
                $Name = 11;
                break;
        }
        return $Name;
    }

    private function GetLevelNum($Level)
    {
        $Name = 'B';
        switch ($Level) {
            case 'A类（重点客户）':
                $Name = 'A';
                break;
            case 'C类（非优先客户）':
                $Name = 'C';
                break;
        }
        return $Name;
    }
}